<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="资产明细">
            <el-table
              :loading="false"
              :data="formItem.jieyong"
              border
              class="fixed-table-table"
              size="mini"
              max-height="240px"
            >
              <el-table-column
                label="序号"
                type="index"
                width="50"
                header-align="center"
                align="center"
              />
              <el-table-column v-for="column in insideColumns2" :key="column.key" header-align="center" align="center" :label="column.title" :prop="column.key" :sortable="column.sortable">
                <template slot-scope="scope">
                  <column-render :render="column.render" :row="scope.row" :column="column" />
                </template>
              </el-table-column>
              <el-table-column label="位置" header-align="center" align="center" width="240">
                <template slot-scope="scope">
                  <treeselect
                    v-model="scope.row.jieyongLocation"
                    :multiple="false"
                    :options="availableLocations"
                    placeholder="请选择位置"
                    :append-to-body="true"
                  />
                </template>
              </el-table-column>
              <el-table-column label="使用人" header-align="center" align="center" width="120">
                <template slot-scope="scope">
                  <el-select v-model="scope.row.jieyongUserCode" size="mini" placeholder="请选择使用人">
                    <el-option v-for="item in users" :key="item.username" :label="item.name" :value="item.username" />
                  </el-select>
                </template>
              </el-table-column>
            </el-table>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="">
            <el-button size="mini" @click="btnCancel">取 消</el-button>
            <el-button type="primary" @click="commit">提 交</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
// import PageableTable from '@neu/biz-core/components/PageableTable'
import { distributeCreate as create, distributeDetail as detail } from '@neu/dcam-ui/api/jieyongSL'
import { searchUser as userList } from '@neu/auth-ui/api/user'
import { tree as locationTree } from '@neu/dcam-ui/api/location'
import treeselect from '@neu/biz-core/components/TreeSelect'
const formItem = {
  title: '',
  reason: '',
  remark: '',
  orgCode: '',
  orgName: '',
  jieyong: []
}

export default {
  components: {
    // PageableTable,
    treeselect
  },
  props: {
    taskId: {
      required: false,
      type: String,
      default: 'NULL'
    },
    workOrderNum: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      availableLocations: [],
      users: [],
      formItem: { ...formItem },
      insideColumns2: [
        { title: '资产名称', width: 140, key: 'name' },
        { title: '资产编号', width: 150, key: 'code' },
        { title: '资产分类', width: 300, key: 'type',
          render: (h, params) => {
            let txt = params.row.smallTypeName
            if (params.row.middleTypeName) {
              txt = params.row.middleTypeName + ' / ' + txt
            }
            if (params.row.bigTypeName) {
              txt = params.row.bigTypeName + ' / ' + txt
            }
            return h('span', txt)
          }
        },
        { title: '规格型号', width: 140, key: 'specifications' },
        { title: '序列号', key: 'serialnumber' }

      ],
      rules: {},
      selectTypeCode: '',
      selectSpecifications: ''
    }
  },
  created() {
    this.fetch()
    // 位置树
    locationTree('sl').then(response => {
      this.availableLocations = response.data
    })
  },
  methods: {
    fetch() {
      if (this.workOrderNum !== 'NULL') {
        detail(this.workOrderNum, this.taskId).then(response => {
          this.formItem = { ...response.data }
          userList(response.data.orgCode).then(res => {
            this.users = res.data
          })
        })
      }
    },
    commit() {
      this.$refs.myform.validate((valid) => {
        if (valid) {
          const data = []
          const locations = this.flattenTreeDataClosure(this.availableLocations)
          if (this.formItem.jieyong.some(it => !it.jieyongUserCode)) {
            return this.$message({
              showClose: true,
              message: '请选择使用人!',
              type: 'error'
            })
          }
          if (this.formItem.jieyong.some(it => !it.jieyongLocation)) {
            return this.$message({
              showClose: true,
              message: '请选择位置!',
              type: 'error'
            })
          }
          this.formItem.jieyong.forEach(l => {
            const item = { ...l }

            item.workOrderNum = this.workOrderNum
            item.taskId = this.taskId

            const one = this.users.find(s => s.username === item.jieyongUserCode)
            item.jieyongUserName = one ? one.name : ''

            // 位置
            const location = locations.find(l => l.code === item.jieyongLocation)
            item.jieyongLocationName = location ? location.title : ''
            data.push(item)
          })

          create(data).then(res => {
            if (res.status === 'success') {
              this.$emit('cancelNewOrderForm', 'refresh')
            }
          })
        }
      })
    },
    btnCancel() {
      this.$emit('cancelNewOrderForm', 'refresh')
    },
    // 扁平化树形结构
    flattenTreeDataClosure(data) {
      const treeData = data
      const flattenData = []
      function flattenTree(data, parentKey) {
        data.forEach(ele => {
          const { title, code, children } = ele
          flattenData.push({ title, code, parentKey })
          if (children) {
            flattenTree(children, code)
          }
        })
      }
      flattenTree(treeData, null)
      return flattenData
    }
  }
}
</script>

<style lang="scss">
  .vue-treeselect.vue-treeselect--append-to-body {
    z-index: 9999!important;
  }
</style>
